Learn how use frontend custom events to modify the Autocomplete menu and InstantSearch results page in the Adobe Commerce and Magento Open Source extension.
algoliaBundle.js
file holds the Autocomplete and InstantSearch libraries and their dependencies.
They’re also accessible from the global algoliaBundle
and are added to most event hooks for access.
If you’re using RequireJS, you can load the algoliaBundle
as a dependency.
The search-insights.js
library is a standalone for the insights features like Click and Conversion Analytics and Personalization.
algoliaConfig
.
The data for this object returns from the block method Algolia\AlgoliaSearch\Block\Configuration::getConfiguration()
and is set in the configuration.phtml
template file.
The Autocomplete and InstantSearch implementations refer to this global variable to build their UI.
To inspect the algoliaConfig
object, type algoliaConfig
in your browser’s console when on your Magento store.
In this object, you can see references for search credentials, index name prefix, display settings for Autocomplete and InstantSearch, facets configuration, and other options related to the search UI.
This is a helpful reference to understand what’s built on the frontend and for your customizations.
script
tags. This had several disadvantages, such as:
window
namespace in your custom implementation, you should first use require
or define
to ensure they’re loaded and ready for your customizations.
For instance, several global objects are loaded by algoliaCommon
, which contains the object algolia
. If you want to reference this object to register a new frontend hook, you must pass algoliaCommon
as a dependency.
For example, the following naked call to the algolia
object is no longer supported:
define
and a declaration in requirejs-config.js
. Refer to the “Where to place your hook” guidelines to implement an algoliaHooks
override to guarantee that Algolia’s libraries (like Autocomplete and InstantSearch) load in the proper sequence after you have registered your custom hooks.
For an example of how to implement this kind of customization, see the CustomAlgolia
boilerplate module on GitHub.
algolia
window variable is an object that handles the setting and firing of these events. (To ensure that the algolia
object is available to your custom implementation, declare algoliaCommon
as a RequireJS dependency.)
Try typing algolia
in your browser’s console when on your Magento store. Your console outputs an object with methods that help trigger events throughout the frontend implementation for Autocomplete, InstantSearch, and Insights.
There are different events available to you depending on your extension version. You can see all available events by entering algolia.allowedHooks
in your browser’s console.
algolia
object is to add your callback function to the available events. To add your hook, use the algolia.registerHook
method, which accepts the parameters of eventName
and the callback function.
beforeWidgetInitialization
event. The event fires from the instantsearch.js
file as:
allWidgetConfiguration
. This variable holds all the InstantSearch widgets configuration already created for you based on your extension settings. Using this event, you can return a modified allWidgetConfiguration
for the InstantSearch implementation.
algoliaHooks
will ensure that your hooks fire at the appropriate time before the InstantSearch or Autocomplete UI is built.
It is recommended to override this file using requirejs-config.js
as demonstrated below:
algolia.registeredHooks
to see all the registered hooks added at that point of the script.
If your hook wasn’t added by this point, run your hook earlier by moving the customization higher in the loading order, but not before common.js
. The extension requires that common.js
runs before to access the algolia
global.
Please remember that hooks don’t need to wait for DOM ready. In fact, running it after DOM ready registers the hooks after the event fires which isn’t ideal.
algolia
object.
To learn how to add a custom JavaScript, see Create a custom extension.
Possible hooks:
afterAutocompleteSources(sources, algoliaClient)
sources
variableafterAutocompletePlugins(plugins, algoliaClient)
plugins
variableafterAutocompleteOptions(options)
options
variableafterAutocompleteProductSourceOptions(options)
options
variableafterAutocompleteStart(algoliaAutocompleteInstance)
algoliaAutocompleteInstance
variablebeforeInstantsearchInit(instantsearchOptions, algoliaBundle)
instantsearch
optionsbeforeWidgetInitialization(allWidgetConfiguration, algoliaBundle)
beforeInstantsearchStart(search, algoliaBundle)
instantsearch
instance before call of start()
methodafterInstantsearchStart(search, algoliaBundle)
instantsearch
instance after call of start()
methodbeforeInstantsearchInit(instantsearchOptions, algoliaBundle)
hook:
toggleRefinement
widget to the InstantSearch page:
allWidgetConfiguration
object and can be removed or modified in this method.
However, some widgets, like hits, can not be added multiple times. In that case, you should add the widget manually in beforeInstantsearchStart
:
afterInsightsBindEvents(algoliaInsights)
algoliaInsights
gives you access to methods for tracking:
trackClick(eventData)
trackView(eventData)
trackConversion(eventData)
eventData
for insights you can use:
buildEventData(eventName, objectID, indexName, position = null, queryID = null)
position
and queryID
facets
parameter that returns the categories attribute which the extension indexes as: categories.level0
or categories.level2
.
The search returns product results as hits, so you need to render the facets instead of products.
You can render the facets in the template suggestions function.
Make sure that your template variables match those returned by the facets to render them in autocomplete.
numericFilters
to the search parameters, in_stock=1
.
To recreate the source value, you need to recreate the options passed into the product source as defined in common.js
.
The numericFilters
returns an array of conditions that includes the new condition.
size
values.
To do this, you need to hook into the event beforeWidgetInitialization
to modify the facet for size.
To sort the values in the refinementList
based on size, add the sortBy
function to the refinementList
. This function lets you match values against each other and sort each value.
To determine the order, use the orderedSizes
array variable defined in the code below. This can be pulled from any source, like Magento or from another index if needed.